package cc.rengu.igas.smps.core.service.trans;

import cc.rengu.igas.smps.common.constant.SmpsParamConstant;
import cc.rengu.igas.smps.common.constant.SmpsServiceConstant;
import cc.rengu.igas.smps.facade.request.BankCardRecogRequest;
import cc.rengu.igas.smps.facade.response.BankCardRecogResponse;
import cc.rengu.jradp.schedule.ScheduleImpl;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

/**
 * OCR银行卡影像识别服务
 * Created by wangcheng on 2021/3/31 15:47
 */
public class OcrBankCardRecogService extends RadpService {
    @Override
    protected int bizInit(String s) {
        return 0;
    }

    @Override
    protected int bizProcess(String s, int i) {
        BankCardRecogRequest bankCardRecogRequest = new BankCardRecogRequest();
        BankCardRecogResponse bankCardRecogResponse = new BankCardRecogResponse();
        try {
            ConvertUtil.convertOutput(bankCardRecogRequest);
            bankCardRecogResponse.setHeader(bankCardRecogRequest.getHeader());
            /* 校验请求参数合法性 */
            if (StringUtil.isEmptyOrNull(bankCardRecogRequest.getImageInfo())) {
                rglog.error("缺少一个或多个业务参数！");
                bankCardRecogResponse.setRespCode(OltpRpcdEnum.PARAM_IS_EMPTY_OR_NULL.getRespCode());
                bankCardRecogResponse.setRespDesc(OltpRpcdEnum.PARAM_IS_EMPTY_OR_NULL.getRespDesc());
            } else {
                /* 演示环境发送短信默认返回成功 */
                if (AppParamConstant.DEFAULT_ENV.equals(AppParamConstant.APPLICATION_ENV)) {
                    bankCardRecogResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
                    bankCardRecogResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
                } else {
                    int iReturnCode = schedule.callService(SmpsServiceConstant.OCR_COMMUNICATION_CLIENT, SmpsParamConstant.DEFAULT_SERVICE_LABEL);
                    if (ScheduleImpl.SERVICE_SUCCESS == iReturnCode) {
                        bankCardRecogResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
                        bankCardRecogResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
                        //TODO 根据具体项目通道返回结果赋值
//                        bankCardRecogResponse.setCardNo("");
//                        bankCardRecogResponse.setCardType("");
//                        bankCardRecogResponse.setCardName("");
//                        bankCardRecogResponse.setBankNo("");
//                        bankCardRecogResponse.setBankName("");
//                        bankCardRecogResponse.setExpireDate("");
                    } else if (ScheduleImpl.SERVICE_TIMEOUT == iReturnCode) {
                        rglog.error("callService {} 失败！返回值={}", SmpsServiceConstant.OCR_COMMUNICATION_CLIENT, iReturnCode);
                        bankCardRecogResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
                        bankCardRecogResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
                    } else {
                        rglog.error("callService {} 失败！返回值={}", SmpsServiceConstant.OCR_COMMUNICATION_CLIENT, iReturnCode);
                        bankCardRecogResponse.setRespCode(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode());
                        bankCardRecogResponse.setRespDesc(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
                    }
                }
            }
            ConvertUtil.convertInput(bankCardRecogResponse);
        } catch (Exception e) {
            rglog.error("{}", cc.rengu.utility.base.StringUtil.ExceptionToString(e));
            bankCardRecogResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            bankCardRecogResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            return -1;
        }
        return 0;
    }

    @Override
    protected int bizTerminate(String s) {
        return 0;
    }
}
